﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Data;
using System.ComponentModel;
using EIA.PM.Model;

namespace EIA.SLClient.PlatManage.PMMgr
{
	public class ConsOrgListViewModel : SimpleMvvmToolkit.ViewModelBase<ConsOrgListViewModel>
	{
		public ConsOrgListViewModel()
		{

            this.RegisterToReceiveMessages(EIA.SLClient.PlatManage.Infrastructure.MessageTokens.ORG_RETRIEVE, EP_Retrieve);
            this.RegisterToReceiveMessages<TOrganization>(EIA.SLClient.PlatManage.Infrastructure.MessageTokens.ORG_NEW_CANCEL, EP_NewCancel);
        }

        private void EP_Retrieve(object sender, SimpleMvvmToolkit.NotificationEventArgs e)
        {
            RetrieveItems();
        }
        private void EP_NewCancel(object sender, SimpleMvvmToolkit.NotificationEventArgs<TOrganization> e)
        {
            if (e.Data != null && OrganizationCollection != null) OrganizationCollection.Remove(e.Data);
        }

        private void Busy(string msg = "等待...")
        {
            IsBusy = true;
            SendMessage<bool>(EIA.SLClient.PlatManage.Infrastructure.MessageTokens.ORG_BUSY_STATE, new SimpleMvvmToolkit.NotificationEventArgs<bool>(msg, true));
        }

        private void Free()
        {
            IsBusy = false;
            SendMessage<bool>(EIA.SLClient.PlatManage.Infrastructure.MessageTokens.ORG_BUSY_STATE, new SimpleMvvmToolkit.NotificationEventArgs<bool>("", false));
        }

        #region notifyprop

        protected bool _IsBusy;
        /// <summary>
        /// 
        /// </summary>
        public bool IsBusy
        {
            get { return _IsBusy; }
            set
            {
                _IsBusy = value;
                NotifyPropertyChanged(m => m.IsBusy);
            }
        }

        protected PagedCollectionView _OrganizationCollection;
        public PagedCollectionView OrganizationCollection
        {
            get { return _OrganizationCollection; }
            set
            {
                _OrganizationCollection = value;
                NotifyPropertyChanged(m => m.OrganizationCollection);
            }
        }

        #endregion

        #region command

        #region RetrieveItemsCommand

        private SimpleMvvmToolkit.DelegateCommand _RetrieveItemsCommand = null;
        public SimpleMvvmToolkit.DelegateCommand RetrieveItemsCommand
        {
            get
            {
                if (_RetrieveItemsCommand == null)
                {
                    _RetrieveItemsCommand = new SimpleMvvmToolkit.DelegateCommand(RetrieveItems, CanExcuteRetrieveItems);
                } return _RetrieveItemsCommand;
            }
            set { _RetrieveItemsCommand = value; NotifyPropertyChanged(m => m.RetrieveItemsCommand); }
        }

        public void RetrieveItems()
        {
            EIA.PM.BLL.ClientProxy.TOrganizationProxy proxy = new EIA.PM.BLL.ClientProxy.TOrganizationProxy();
            Busy();
            proxy.ListAll((lst) =>
            {
                Free();
                OrganizationCollection = new PagedCollectionView(lst);
            }, (ex) =>
            {
                Free();
                System.Windows.MessageBox.Show("获取建设单位列表发生错误！", "提示", System.Windows.MessageBoxButton.OK);
            });
        }

        public bool CanExcuteRetrieveItems()
        {
            return true;
        }

        #endregion

        #region InsertItemCommand

        private SimpleMvvmToolkit.DelegateCommand _InsertItemCommand = null;
        public SimpleMvvmToolkit.DelegateCommand InsertItemCommand
        {
            get
            {
                if (_InsertItemCommand == null)
                {
                    _InsertItemCommand = new SimpleMvvmToolkit.DelegateCommand(InsertItem, CanExcuteInsertItem);
                } return _InsertItemCommand;
            }
            set { _InsertItemCommand = value; NotifyPropertyChanged(m => m.InsertItemCommand); }
        }

        public void InsertItem()
        {
            TOrganization org = new TOrganization();
            org.OrgName = "新单位";

            List<TOrganization> lst = new List<TOrganization>();
            lst.Add(org);
            lst.AddRange(OrganizationCollection.SourceCollection as IList<TOrganization>);
            OrganizationCollection = new PagedCollectionView(lst);

            this.SendMessage<TOrganization>(EIA.SLClient.PlatManage.Infrastructure.MessageTokens.ORG_NEW, new SimpleMvvmToolkit.NotificationEventArgs<TOrganization>("", org));
        }

        public bool CanExcuteInsertItem()
        {
            return true;
        }

        #endregion

        #region UpdateItemCommand

        private SimpleMvvmToolkit.DelegateCommand<EIA.PM.Model.TOrganization> _UpdateItemCommand = null;
        public SimpleMvvmToolkit.DelegateCommand<EIA.PM.Model.TOrganization> UpdateItemCommand
        {
            get
            {
                if (_UpdateItemCommand == null)
                {
                    _UpdateItemCommand = new SimpleMvvmToolkit.DelegateCommand<EIA.PM.Model.TOrganization>(UpdateItem, CanExcuteUpdateItem);
                } return _UpdateItemCommand;
            }
            set { _UpdateItemCommand = value; NotifyPropertyChanged(m => m.UpdateItemCommand); }
        }

        public void UpdateItem(EIA.PM.Model.TOrganization p)
        {
            this.SendMessage<TOrganization>(EIA.SLClient.PlatManage.Infrastructure.MessageTokens.ORG_UPDATE, new SimpleMvvmToolkit.NotificationEventArgs<TOrganization>("", p));
        }

        public bool CanExcuteUpdateItem(EIA.PM.Model.TOrganization p)
        {
            return true;
        }

        #endregion

        #region DeleteItemCommand

        private SimpleMvvmToolkit.DelegateCommand<EIA.PM.Model.TOrganization> _DeleteItemCommand = null;
        public SimpleMvvmToolkit.DelegateCommand<EIA.PM.Model.TOrganization> DeleteItemCommand
        {
            get
            {
                if (_DeleteItemCommand == null)
                {
                    _DeleteItemCommand = new SimpleMvvmToolkit.DelegateCommand<EIA.PM.Model.TOrganization>(DeleteItem, CanExcuteDeleteItem);
                } return _DeleteItemCommand;
            }
            set { _DeleteItemCommand = value; NotifyPropertyChanged(m => m.DeleteItemCommand); }
        }

        public void DeleteItem(EIA.PM.Model.TOrganization p)
        {
            if (System.Windows.MessageBox.Show(string.Format("确认删除单位 [{0}]", p.OrgName), "提示", System.Windows.MessageBoxButton.OKCancel) != System.Windows.MessageBoxResult.OK) return;

            EIA.PM.BLL.ClientProxy.TOrganizationProxy proxy = new EIA.PM.BLL.ClientProxy.TOrganizationProxy();
            Busy();
            proxy.Delete(p.OrgId, (r) =>
            {

                Free();
                if (r)
                {
                    OrganizationCollection.Remove(p);
                }
                else
                {
                    // 未删除
                    System.Windows.MessageBox.Show("删除建设单位失败！", "提示", System.Windows.MessageBoxButton.OK);
                }

            }, (ex) =>
            {
                Free();
                System.Windows.MessageBox.Show("删除建设单位发生错误！", "提示", System.Windows.MessageBoxButton.OK);
            });
        }

        public bool CanExcuteDeleteItem(EIA.PM.Model.TOrganization p)
        {
            return true;
        }

        #endregion

        #endregion

        public void OnLoad()
        {
            if (OrganizationCollection == null) RetrieveItems();
        }
	}
}